﻿using HuaCheng.DataAccess;
using HuaCheng.CommonClass;
using System.Collections.Generic;
using System.Drawing;
using System.Text;
using System;
namespace HuaCheng.Controller
{
    public class IniControl
    {
        /// <summary>
        /// 根据ini读取数据计算对应颜色
        /// </summary>
        /// <param name="panelName"></param>
        /// <returns></returns>
        public static Color CalColor(string panelName)
        {
            try
            {
                StringBuilder temp = new StringBuilder(500);
                string path = DicHelper.RetrieveConfigPath();
                int i = ReadAndWriteIni.GetPrivateProfileString("step color", panelName, "", temp, 500, path);
                int blue = Convert.ToInt32(temp.ToString()) / 65536;
                int green = Convert.ToInt32(temp.ToString()) % 65536 / 256;
                int red = Convert.ToInt32(temp.ToString()) - (65536 * blue) - (256 * green);
                return Color.FromArgb(255, red, green, blue);               
            }
            catch(Exception e)
            {
                Console.WriteLine(e.StackTrace + e.Message);
                return Color.White;
            }            
        }
        /// <summary>
        /// 根据ini读取设置的单位
        /// </summary>
        /// <returns></returns>
        public static Dictionary<string, string> ReadCustom(string strSec)
        {
            List<string> itemName = new List<string>() { "CommonTime", "StepTime", "Pressure", "Energy", "Time", "Power", "Res", "Temp", "Cap", "Current", "Voltage" };
            Dictionary<string, string> dicCustom = new Dictionary<string, string>();
            StringBuilder temp = new StringBuilder(500);
            string path = DicHelper.RetrieveConfigPath();
            for (int i = 0; i < itemName.Count; i++)
            {
                int r = ReadAndWriteIni.GetPrivateProfileString(strSec, itemName[i], "", temp, 500, path);
                dicCustom.Add(itemName[i], Convert.ToString(temp));
            }
            return dicCustom;
        }
        /// <summary>
        /// 读取上一次设置选中为默认单位还是定制单位
        /// </summary>
        /// <returns></returns>
        public static string ReadMode()
        {
            StringBuilder temp = new StringBuilder(500);
            string path = DicHelper.RetrieveConfigPath();
            int r = ReadAndWriteIni.GetPrivateProfileString("unit custom", "Mode", "", temp, 500, path);
            return Convert.ToString(temp);
        }
    }
}
